package com.kecheng.beik;

import com.kecheng.action.BaseAction;
import com.kecheng.com.yx.pojo.AccessToken;
import com.kecheng.com.yx.util.WeixinUtil;
import com.kecheng.dao.ProductDao;
import com.kecheng.dao.YongjinDao;
import com.kecheng.pojo.Product;
import com.kecheng.pojo.Team;
import com.kecheng.pojo.Yongjin;
import com.kecheng.util.DownLoad;
import net.sf.json.JSONObject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Action(value = "team", results = {
		@Result(name = "ziliao", location = "/w/ziliao.jsp"),
		@Result(name = "erweima", location = "/w/erweima.jsp"),
		@Result(name = "update", location = "/w/address.jsp"),
		@Result(name = "my", location = "/w/yonghu.jsp"),
		@Result(name = "findall", location = "/admin/seller/allTeam.jsp"),
		@Result(name = "fxteam", location = "/admin/seller/fxseller.jsp"),
		@Result(name = "order", location = "/admin/seller/paihang.jsp"),
		@Result(name = "findbyid", location = "/admin/seller/teamDetail.jsp"),
		@Result(name = "money", location = "/w/money.jsp"),
		@Result(name = "tixian1", location = "/w/tixian1.jsp"),

})
@AllowedMethods(value = {("regex:.*")})
public class TeamAction extends BaseAction {

	private List<Team> teams;
	private int[] id;
	private int key;
	private Product product;
	@Autowired
	private ProductDao productDao;
	private List<Team> teams1;
	private List<Team> teams2;
	private List<Team> teams3;
	private int num1;
	private int num2;
	private int num3;
	private String imagecode;
	private File file;
	private String fileFileName;
	private String fileContentType;
	private List<Product> products;
	private String isQiandao;
	private Yongjin yongjin;
	@Autowired
	private YongjinDao yongjinDao;


	public String findteam() {
		openid = (String) session.get("openid");
		team = teamDao.findbysellerid(openid);
		teams1 = teamDao.findbyrefereeid(team.getId());
		num1 = teams1.size();
		teams2 = teamDao.findbytree(team.getId());
		num2 = teams2.size();
		teams3 = teamDao.findbyfour(team.getId());
		num3 = teams3.size();
		return "tuandui";
	}

	public String tomoney() throws IOException {
		openid = (String) session.get("openid");
		team = teamDao.findbysellerid(openid);
		return "money";
	}

	public String update22() {
		openid=(String) session.get("openid");
		Team team1 = teamDao.findbysellerid(openid);
		team1.setName(team.getName());
		team1.setPhone(team.getPhone());
		team1.setAddress(team.getAddress());
		////System.out.println(team1.getName());
		teamDao.update(team1);
		team = team1;
		return "ziliao";
	}

	public String update2() {
		openid = (String) session.get("openid");
		team = teamDao.findbysellerid(openid);
		return "xiugai";
	}

	public String find() {
		openid = (String) session.get("openid");
		team = teamDao.findbysellerid(openid);
		return "ziliao";
	}

	public String paihangjf() {
		teams = teamDao.findpaihangjf();
		return "paihangjf";
	}

	public String jiazu() throws IOException {
		try{
		loadOpenid();
		}catch(Exception e){
			e.printStackTrace();
			return "biaoqing";
		}
		////System.out.println("openid:"+openid);
		team = teamDao.findbysellerid(openid);
		////System.out.println("team="+team);
		
		return "my";
	}

	public String erweima() throws IOException {
			loadOpenid();
			if (team.getErweima() == null || team.getErweima().equals("")) {
				int a = team.getId();
				String u = "{\"action_name\": \"QR_LIMIT_SCENE\",\"action_info\": {\"scene\": {\"scene_id\":"
						+ a + "}}}";
				try {
					AccessToken at = WeixinUtil.getAccessToken();
					
					String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="
							+ at.getToken();
					JSONObject jsonObject = WeixinUtil.httpRequest(url,"POST", u);
					String ticket = (String) jsonObject.get("ticket");

					String url1 = "http://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="
							+ ticket;
					String jieguo = DownLoad.downloadCode(url1, openid, root);
					team.setErweima("upload/" + jieguo);
					teamDao.update(team);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return "erweima";
	}

	public String search1() {
		teams = teamDao.search(key);
		allpage = 1;
		return "teams";
	}

	public String findbyid() {
		team = teamDao.findbyid(team.getId());
		Team team1 = teamDao.findbyid(team.getRefereeid());
		if (team1 == null) {
			team.setRname("无推荐人");
		} else {
			team.setRname(team1.getName());
		}
		return "findbyid";
	}

	public String findbyrefereeid1() {

		// String sellerid=(String) session.get("openId");
		// team=teamDao.findbysellerid(sellerid);
		teams = teamDao.findbyrefereeid(team.getId());
		return "tuandui1";
	}

	public String findbytree() {

		teams = teamDao.findbytree(team.getId());
		return "fxteam";

	}

	public String findbytree1() {

		// String sellerid=(String) session.get("openId");
		// team=teamDao.findbysellerid(sellerid);
		teams = teamDao.findbytree(team.getId());

		return "tuandui1";

	}

	public String findbyfour() {
		teams = teamDao.findbyfour(team.getId());
		return "fxteam";

	}

	public String findbyfour1() {
		// String sellerid=(String) session.get("openId");
		// team=teamDao.findbysellerid(sellerid);
		teams = teamDao.findbyfour(team.getId());

		return "tuandui1";

	}

	public String findbyrefereeid() {
		teams = teamDao.findbyrefereeid(team.getId());
		////System.out.println(team.getId());
		return "fxteam";
	}

	public String deletebyid() {

		////System.out.println(id[0]);

		for (int i = 0; i < id.length; i++) {
			try {
				team = teamDao.findbysid(id[i]);
				teamDao.delete(team);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return findall();
	}

	public String findall() {
		if (page == null) {
			page = 1;
		}
		teams = teamDao.findall(page);
		allpage = teamDao.findallpage();
		return "findall";
	}

	public String order() {
		if (page == null) {
			page = 1;
		}
		teams = teamDao.order(page);
		allpage = teamDao.findallpage();
		return "order";
	}


	public List<Team> getTeams() {
		return teams;
	}

	public void setTeams(List<Team> teams) {
		this.teams = teams;
	}

	public int[] getId() {
		return id;
	}

	public void setId(int[] id) {
		this.id = id;
	}

	public int getKey() {
		return key;
	}
	public void setKey(int key) {
		this.key = key;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public List<Team> getTeams1() {
		return teams1;
	}

	public void setTeams1(List<Team> teams1) {
		this.teams1 = teams1;
	}

	public List<Team> getTeams2() {
		return teams2;
	}

	public void setTeams2(List<Team> teams2) {
		this.teams2 = teams2;
	}

	public List<Team> getTeams3() {
		return teams3;
	}

	public void setTeams3(List<Team> teams3) {
		this.teams3 = teams3;
	}

	public int getNum1() {
		return num1;
	}

	public void setNum1(int num1) {
		this.num1 = num1;
	}

	public int getNum2() {
		return num2;
	}

	public void setNum2(int num2) {
		this.num2 = num2;
	}

	public int getNum3() {
		return num3;
	}

	public void setNum3(int num3) {
		this.num3 = num3;
	}

	public String getImagecode() {
		return imagecode;
	}

	public void setImagecode(String imagecode) {
		this.imagecode = imagecode;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}


	public String getIsQiandao() {
		return isQiandao;
	}


	public void setIsQiandao(String isQiandao) {
		this.isQiandao = isQiandao;
	}
	public Yongjin getYongjin() {
		return yongjin;
	}
	public void setYongjin(Yongjin yongjin) {
		this.yongjin = yongjin;
	}

}
